/*
 * bat.c
 *
 * A bit of a stopgap in making better bat structures in C.
 *
 * $Id: bat.c 927 2006-09-03 06:17:24Z rpz $
 *
 * Bob Zimmermann <rpz@cse.wustl.edu> Totally free.
 *
 */

#include "config.h"
#include "BAT_Annotation.h"
#include "BAT_Parser.h"
#include "BAT_Builder.h"
#include "BAT_Validator.h"
#include "BAT_Writer.h"
#include <libgen.h>

void
welcome()
{
  fprintf(stderr, "\n");
  fprintf(stderr, "Biological Annotation Tool (BAT) v%s %s\n", VERSION,
	  BAT_BUILD_DATE);
  fprintf(stderr, "Copyright (C) 2006 Washington University in St. Louis\n");
  fprintf(stderr, "\n");
}

void
usage(char *progname)
{
  fprintf(stderr, "\n");
  fprintf(stderr, "Usage: %s [ OPTIONS ] gtf-file\n", basename(progname));
  fprintf(stderr, "\n");
  fprintf(stderr, "       Takes a gtf-file and merges identical \n");
  fprintf(stderr, "       transcripts into genes.  Fast.\n");
  fprintf(stderr, "\n");
  exit(1);
}

int
main(int argc, char *argv[])
{
  BAT_GeneTab *gt;
  BAT_Parser *p;
  BAT_Builder *b;
  BAT_Writer *w;
  BAT_Validator *v;
  BAT_Free_Feature **feats;
  unsigned n_feats;

  welcome();

  if (argc < 2)
    usage(argv[0]);

  fprintf(stderr, "Parsing ...\n");
  p = BAT_Parser_new();
  BAT_Parser_parsefile_by_name(p, argv[1]);
  feats = BAT_Parser_claim_features(p, &n_feats);
  BAT_Parser_delete(p, 1);

  fprintf(stderr, "Building ...\n");
  b = BAT_Builder_new();
  BAT_Builder_add_features(b, feats, n_feats);

  gt = BAT_Builder_build_gene_table(b);
  BAT_Builder_delete(b, 1);

  fprintf(stderr, "Validating ... \n");
  v = BAT_Validator_new();
  BAT_Validator_add_command(v, "geneid --enable --cds-only");
  gt = BAT_Validator_validate_gene_table(v, gt);
  BAT_Validator_delete(v, 1);

  fprintf(stderr, "Writing ...\n");
  w = BAT_Writer_new();
  BAT_Writer_write_to_fh(w, stdout, BAT_GTF_Writer_Type, gt, BAT_GTF_Obj);
  BAT_Writer_delete(w, 1);
  BAT_GeneTab_delete(gt, 1);

  return 0;
}
